package org.wxd.ms.stu.aggre.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.wxd.ms.stu.aggre.domain.User;
import rx.Observable;

import javax.annotation.Resource;

/**
 * Created by wangxd on 2017/6/12.
 */
@Service
public class UserAggreService {
    @Resource
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "fallback")
    public Observable<User> getUserById(Long id){
        return Observable.create(observer -> {
           User user = restTemplate.getForObject("http://stu-service/user/{id}",User.class,id);
           observer.onNext(user);
           observer.onCompleted();
        });
    }


    @HystrixCommand(fallbackMethod = "fallback")
    public Observable<User> getUser(Long id){
        return Observable.create(observer -> {
            User user = restTemplate.getForObject("http://stu-consumer/user/{id}",User.class,id);
            observer.onNext(user);
            observer.onCompleted();
        });
    }

    public User fallback(Long id){
        User user = new User();
        user.setName("default-zuul");
        return user;
    }
}
